package cc.mango.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class StringUtils
{
	public static Calendar	calendar	= Calendar.getInstance();

	public static String inputStreamToString(final InputStream stream)
			throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(stream));
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = br.readLine()) != null)
		{
			sb.append(line + "\n");
		}
		br.close();
		return sb.toString();
	}

	public static String getTodayByFormat(String format)
	{
		try
		{
			Date date = new Date();
			if (format == null)
				format = "yyyyMMddHHmmss";
			SimpleDateFormat formatter = new SimpleDateFormat(format);
			String dateString = formatter.format(date);
			return dateString;
		}
		catch (Exception e)
		{
		}
		return "";
	}
	
	public static String getYesterdayByFormat(String format)
	{
		try
		{
			Date date = new Date();
			date.setDate(date.getDate()-1);
			if (format == null)
				format = "yyyyMMddHHmmss";
			SimpleDateFormat formatter = new SimpleDateFormat(format);
			String dateString = formatter.format(date);
			return dateString;
		}
		catch (Exception e)
		{
		}
		return "";
	}

	public static String date2String(Date date, String format)
	{
		if (format == null)
			format = "yyyyMMddHHmmss";
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	public static Date string2Date(String date, String format)
	{
		if (format == null)
			format = "yyyyMMddHHmmss";
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		try
		{
			return formatter.parse(date);
		}
		catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static String getLastWeekDate(String format)
	{
		int day = 6;
		Date today = /*string2Date(getTodayByFormat(format),format);*/new Date();
		return date2String(getDateBefore(today, day), format);
	}

	/**
	 * 得到几天前的时间
	 * @param d
	 * @param day
	 * @return
	 */
	public static Date getDateBefore(Date d, int day)
	{
		//	     Calendar now =Calendar.getInstance();
		calendar.setTime(d);
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - day);
		return calendar.getTime();
	}

	/**
	 * 得到几天后的时间
	 * @param d
	 * @param day
	 * @return
	 */
	public static Date getDateAfter(Date d, int day)
	{
		//	     Calendar now =Calendar.getInstance();
		calendar.setTime(d);
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
		return calendar.getTime();
	}

	public static String getLastMonthDate()
	{
		//Calendar cal = Calendar.getInstance();
		/*	calendar.add(Calendar.MONTH, -1);
			calendar.add(Calendar.DATE, +1);
			int month = calendar.get(Calendar.MONTH) + 1;
			String im = month+"";
			if(month < 10)
				im = "0"+month;
			return calendar.get(Calendar.YEAR) + "-" + im + "-" + calendar.get(Calendar.DATE);*/

		//-----------//modified  by haibo  on 2011-03-25---------------------
		int day = 30;
		Date today = new Date();
		return date2String(getDateBefore(today, day),
				FinalKey.YEAR_MONTH_DAY_FORMAT);
		//-----------//modified  by haibo  on 2011-03-25---------------------

	}

	public static String getLast3MonthsDate()
	{
		//Calendar cal = Calendar.getInstance();
		/*	calendar.add(Calendar.MONTH, -3);
			calendar.add(Calendar.DATE, +1);
			int month = calendar.get(Calendar.MONTH) + 1;
			String im = month+"";
			if(month < 10)
				im = "0"+month;
			return calendar.get(Calendar.YEAR) + "-" + im + "-" + calendar.get(Calendar.DATE);*/

		//-----------//modified  by haibo  on 2011-03-25---------------------
		int day = 30 * 3;
		Date today = new Date();
		return date2String(getDateBefore(today, day),
				FinalKey.YEAR_MONTH_DAY_FORMAT);
		//-----------//modified  by haibo  on 2011-03-25---------------------
	}

	public static String getLast6MonthsDate()
	{
		//		Calendar cal = Calendar.getInstance();
		/*	calendar.add(Calendar.MONTH, -6);
			calendar.add(Calendar.DATE, +1);
			int month = calendar.get(Calendar.MONTH) + 1;
			String im = month+"";
			if(month < 10)
				im = "0"+month;
			return calendar.get(Calendar.YEAR) + "-" + im + "-" + calendar.get(Calendar.DATE);*/

		//-----------//modified  by haibo  on 2011-03-25---------------------
		int day = 30 * 6;
		Date today = new Date();
		return date2String(getDateBefore(today, day),
				FinalKey.YEAR_MONTH_DAY_FORMAT);
		//-----------//modified  by haibo  on 2011-03-25---------------------
	}

	public static String getLast2Years()
	{
		//		Calendar cal = Calendar.getInstance();
		/*calendar.add(Calendar.YEAR, -2);
		calendar.add(Calendar.MONTH, -2);
		calendar.add(Calendar.DATE, -10);
		int month = calendar.get(Calendar.MONTH) + 1;
		String im = month+"";
		if(month < 10)
			im = "0"+month;
		return calendar.get(Calendar.YEAR) + "-" + im + "-" + calendar.get(Calendar.DATE);*/

		//-----------//modified  by haibo  on 2011-03-25---------------------
		int day = 365 * 2;
		Date today = new Date();
		return date2String(getDateBefore(today, day),
				FinalKey.YEAR_MONTH_DAY_FORMAT);
		//-----------//modified  by haibo  on 2011-03-25---------------------
	}

	public static String getLastYearDate()
	{
		/*Calendar cal = Calendar.getInstance();*/
		/*calendar.add(Calendar.YEAR, -1);
		calendar.add(Calendar.DATE, +1);
		int month = calendar.get(Calendar.MONTH) + 1;
		String im = month+"";
		if(month < 10)
			im = "0"+month;
		return calendar.get(Calendar.YEAR) + "-" + im + "-" + calendar.get(Calendar.DATE);*/

		//-----------//modified  by haibo  on 2011-03-25---------------------
		int day = 365;
		Date today = new Date();
		return date2String(getDateBefore(today, day),
				FinalKey.YEAR_MONTH_DAY_FORMAT);
		//-----------//modified  by haibo  on 2011-03-25---------------------
	}

	public static String getLast2YearsDate()
	{
		//		Calendar calendar = Calendar.getInstance();
		/*calendar.add(Calendar.YEAR, -2);
		calendar.add(Calendar.DATE, +1);
		int month = calendar.get(Calendar.MONTH) + 1;
		String im = month+"";
		if(month < 10)
			im = "0"+month;
		return calendar.get(Calendar.YEAR) + "-" + im + "-" + calendar.get(Calendar.DATE);*/

		//-----------//modified  by haibo  on 2011-03-25---------------------
		int day = 365 * 2;
		Date today = new Date();
		return date2String(getDateBefore(today, day),
				FinalKey.YEAR_MONTH_DAY_FORMAT);
		//-----------//modified  by haibo  on 2011-03-25---------------------
	}

	/*
	 * 
	 * by dan
	 * 
	 * 
	 */
	// DT, 2011/02/01, format a positive numeric value to follow 5 chars rule,
	// maximum length of formatted numeric value is 5 characters, 
	// including decimal point, without plus sign
	public static String formatToFiveCharsRuleWithoutSign(String source)
	{
		final String DECIMAL_POINT = ".";
		final String ZERO = "0";

		String result = null;

		// prepare formatter with 0, 1, 2, 3 d.p. respectively
		NumberFormat formaterOnSacle0 = DecimalFormat.getInstance();
		NumberFormat formaterOnSacle1 = DecimalFormat.getInstance();
		NumberFormat formaterOnSacle2 = DecimalFormat.getInstance();
		NumberFormat formaterOnSacle3 = DecimalFormat.getInstance();

		// disable thousands-separators
		formaterOnSacle0.setGroupingUsed(false);
		formaterOnSacle1.setGroupingUsed(false);
		formaterOnSacle2.setGroupingUsed(false);
		formaterOnSacle3.setGroupingUsed(false);

		// set the number of digits after decimal point
		formaterOnSacle0.setMaximumFractionDigits(0);
		formaterOnSacle0.setMinimumFractionDigits(0);

		formaterOnSacle1.setMaximumFractionDigits(1);
		formaterOnSacle1.setMinimumFractionDigits(1);

		formaterOnSacle2.setMaximumFractionDigits(2);
		formaterOnSacle2.setMinimumFractionDigits(2);

		formaterOnSacle3.setMaximumFractionDigits(3);
		formaterOnSacle3.setMinimumFractionDigits(3);

		try
		{
			double dvalue = Double.parseDouble(source);

			// format to corresponding d.p. according to value range
			if (dvalue >= 1000.0000)
			{
				result = formaterOnSacle0.format(dvalue);

			}
			else if (dvalue >= 100.0000 && dvalue <= 999.9999)
			{
				result = formaterOnSacle1.format(dvalue);

			}
			else if (dvalue >= 10.0000 && dvalue <= 99.9999)
			{
				result = formaterOnSacle2.format(dvalue);

			}
			else if (dvalue >= 0.0000 && dvalue <= 9.9999)
			{
				result = formaterOnSacle3.format(dvalue);

			}

			// special handling for possible 1000.0 case
			// remove trailing .0
			if (result.endsWith(".0") && (result.length() == 6))
			{
				result = result.substring(0, result.length() - 2);
			}

			// remove trailer zeroes for values with decimal point
			// logic: keep removing "0" from the end until it is a non-zero character
			if (result.indexOf(DECIMAL_POINT) != -1)
			{
				while (result.lastIndexOf(ZERO) == result.length() - 1)
				{
					result = result.substring(0, result.length() - 1);
				}

				// add back a zero after decimal point if it is non-zero value
				if (result.lastIndexOf(DECIMAL_POINT) == result.length() - 1)
				{
					if (result.equals("0."))
					{
						result = "0";
					}
					else
					{
						result = result + ZERO;
					}
				}
			}

		}
		catch (Exception e)
		{
			result = "";
		}

		return result;
	}
}
